package com.ghck.server.controller.shop;

import java.security.Principal;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ghck.server.GhckAplication;
import com.ghck.server.exception.ErrorConstant;
import com.ghck.server.exception.GhckException;
import com.ghck.server.model.response.Result;
import com.ghck.server.model.response.ResultList;
import com.ghck.server.security.AjkUserDetails;
import com.ghck.server.utils.AjkUtil;
import com.ghck.shop.model.Bank;
import com.ghck.shop.model.FlowWater;
import com.ghck.shop.model.TiXian;
import com.ghck.shop.service.OperateService;

@RestController
@RequestMapping(value="/operate")
public class OperateController {
	
	@Autowired
	private OperateService operateService;
	
	@PreAuthorize(GhckAplication.shop_maker_user)
	@RequestMapping(value="/myFlowWater",method=RequestMethod.GET)
	public ResultList<FlowWater> myFlowWater(Principal principal,FlowWater flowWater,
			@RequestParam(defaultValue = "0") Integer start,
            @RequestParam(defaultValue = "20") Integer limit){
		ResultList<FlowWater> result = new ResultList<FlowWater>();
		AjkUserDetails userDetails = AjkUtil.getUserDetails(principal);
		flowWater.setUserId(userDetails.getUserId());
		flowWater.setStart(start);
		flowWater.setLimit(limit);
		List<FlowWater> list = operateService.listMyFlowWaters(flowWater);
		result.setItemList(list);
		return result;
	}

	/**
	 * 所有银行列表
	 * @return
	 */
	@PreAuthorize(GhckAplication.all)
	@RequestMapping(value="/allBanks",method=RequestMethod.GET)
	public ResultList<Bank> allBanks(){
		ResultList<Bank> result = new ResultList<Bank>();
		result.setItemList(operateService.allBanks());
		return result;
	}
	
	@PreAuthorize(GhckAplication.shop_maker)
	@RequestMapping(value="/tixian",method=RequestMethod.POST)
	public Result tixian(Principal principal,@RequestBody TiXian tiXian){
		if(StringUtils.isEmpty(tiXian.getCardNo())){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "银行卡号不能为空");
		}
		if(StringUtils.isEmpty(tiXian.getName())){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "提现人名称不能为空");
		}
		if(StringUtils.isEmpty(tiXian.getBankName())){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "银行名称不能为空");
		}
		if(tiXian.getBankId()==null || tiXian.getBankId() <=0){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "银行ID不能为空");
		}
		if(tiXian.getAmount()==null || tiXian.getAmount() <=0){
			throw new GhckException(ErrorConstant.PARAM_ERROR.getErrorCode(), "提现金额不能为空");
		}
		AjkUserDetails userDetails = AjkUtil.getUserDetails(principal);
		tiXian.setUserId(userDetails.getUserId());
		Result result = new Result();
		operateService.tixian(tiXian);
		return result;
	}
}
